package com.junglone.gateway.admin.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.junglone.gateway.admin.common.config.Global;
import com.junglone.gateway.admin.domain.SysUser;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Description: 首页 业务处理
 *
 * @author junglone: <a href="mailto:cjlnzb@hotmail.com">cjlnzb@hotmail.com</a>
 * @since 2021/03/31 15:25
 */
@Controller
public class SysIndexController {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(SysIndexController.class);

    @Resource
    private Global global;

    /**
     * Description: 系统首页
     *
     * @param model model
     *
     * @return java.lang.String
     *
     * @author junglone: <a href="mailto:cjlnzb@hotmail.com">cjlnzb@hotmail.com</a>
     * @since 2021/03/31 15:25
     */
    @GetMapping("/index")
    public String index(Model model, WebSession webSession) throws JsonProcessingException {

        Map<String, Object> mapSession = webSession.getAttributes();
        LOGGER.info("index ====== mapSession ==== {}", mapSession);

        // 取身份信息
        SysUser user = new SysUser();
        user.setUserName("admin");
        user.setLoginName("hello");
        user.setAvatar("");
        // 根据用户id取出菜单
        // List<SysMenu> menus = menuService.selectMenusByUser(user);
        // mmap.put("menus", null);

        Mono<SysUser> monoUser = Mono.just(user);
        Mono<Global> monoGlobal = Mono.just(global);
        model.addAttribute("user", monoUser);
        model.addAttribute("global", monoGlobal);
        return "index";
    }

    /**
     * Description: 系统介绍
     *
     * @return java.lang.String
     *
     * @author junglone: <a href="mailto:cjlnzb@hotmail.com">cjlnzb@hotmail.com</a>
     * @since 2021/03/31 15:23
     */
    @GetMapping("/system/main")
    public String main() {
        return "main";
    }

    /**
     * Description: hello
     *
     * @return java.lang.String
     *
     * @author junglone: <a href="mailto:cjlnzb@hotmail.com">cjlnzb@hotmail.com</a>
     * @since 2021/03/31 17:15
     */
    @GetMapping("/system/hello")
    public String hello(Model model) {
        // 数组
        Flux<String> flux = Flux.just("hello flux");
        // 实例
        Mono<String> mono = Mono.just("hello mono");

        // 无效设置
        model.addAttribute("hello", "hello attribute");

        model.addAttribute("flux", flux);
        model.addAttribute("mono", mono);
        return "system/hello";
    }
}
